home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Python 1.3.3 / Python 133 SRC / Demo / www / logsumm.py < prev    next >
Text File  |  1996-03-12  |  3KB  |  138 lines

  1. #! /usr/local/bin/python
  2.  
  3. import sys
  4. import getopt
  5. import string
  6.  
  7. def main():
  8.     try:
  9.         opts, args = getopt.getopt(sys.argv[1:], '')
  10.     except getopt.error, msg:
  11.         sys.stdout = sys.stderr
  12.         print msg
  13.         sys.exit(2)
  14.     file = '/ufs/guido/src/www/Log-inet'
  15.     if args: file = args[0]
  16.     if file == '-':
  17.         fp = sys.stdin
  18.     else:
  19.         try:
  20.             fp = open(file, 'r')
  21.         except IOError, msg:
  22.             sys.stdout = sys.stderr
  23.             print file, ': I/O Error:', msg
  24.             sys.exit(1)
  25.     process(fp)
  26.  
  27. def process(fp):
  28.     per_file = {}
  29.     per_host = {}
  30.     per_day = []
  31.     errors_per_day = []
  32.     last_day = None
  33.     lineno = 0
  34.     while 1:
  35.         line = fp.readline()
  36.         if not line: break
  37.         lineno = lineno + 1
  38.         words = string.split(line)
  39.         if len(words) < 5:
  40.             print 'line', lineno, ': bad line :', line,
  41.             continue
  42.         [month, day, time, year] = words[:4]
  43.         # Count requests per day
  44.         if (year, month, day) <> last_day:
  45.             do_header = 0
  46.             if last_day:
  47.                 print last_day[1], last_day[2], len(per_day),
  48.                 if errors_per_day:
  49.                     print '(', len(errors_per_day),
  50.                     print 'errors )',
  51.                 print
  52.                 if (year, month) <> last_day[:2]:
  53.                     print
  54.                     print '****** Summary of',
  55.                     print last_day[1], last_day[0],
  56.                     print '******'
  57.                     show_per_file(per_file)
  58.                     show_per_host(per_host)
  59.                     per_file = {}
  60.                     per_host = {}
  61.                     do_header = 1
  62.             else:
  63.                 do_header = 1
  64.             if do_header:
  65.                 print
  66.                 print '****** Requests per day in',
  67.                 print month, year, '******'
  68.             per_day = []
  69.             errors_per_day = []
  70.             last_day = (year, month, day)
  71.         if len(words) == 8 and words[4] == 'sending' and \
  72.               words[6] == 'to':
  73.             file = words[5]
  74.             host = words[7]
  75.             per_day.append(time, file, host)
  76.             if not per_file.has_key(file):
  77.                 per_file[file] = []
  78.             per_file[file].append(year, month, day, time, host)
  79.             if not per_host.has_key(host):
  80.                 per_host[host] = []
  81.             per_host[host].append(year, month, day, time, file)
  82.         else:
  83.             errors_per_day.append(time, words[4:])
  84.     print
  85.     print '****** Summary of',
  86.     print last_day[1], last_day[0],
  87.     print '******'
  88.     show_per_file(per_file)
  89.     show_per_host(per_host)
  90.  
  91. def show_per_file(per_file):
  92.     print '====== top scoring files ======'
  93.     files = per_file.keys()
  94.     score = []
  95.     for file in files:
  96.         score.append(len(per_file[file]), file)
  97.     score.sort()
  98.     score.reverse()
  99.     for count, file in score:
  100.         print string.rjust(`count`, 3), file,
  101.         (year, month, day, time, host) = per_file[file][0]
  102.         print '(', day, realname(host),
  103.         if len(per_file[file]) > 1:
  104.             (year, month, day, time, host) = per_file[file][-1]
  105.             print day,
  106.         print ')'
  107.  
  108. def show_per_host(per_host):
  109.     print '====== top scoring hosts ======'
  110.     hosts = per_host.keys()
  111.     score = []
  112.     for host in hosts:
  113.         score.append(len(per_host[host]), host)
  114.     score.sort()
  115.     score.reverse()
  116.     for count, host in score:
  117.         print string.rjust(`count`, 3), realname(host),
  118.         (year, month, day, time, file) = per_host[host][0]
  119.         print '(', day, file,
  120.         if len(per_host[host]) > 1:
  121.             (year, month, day, time, file) = per_host[host][-1]
  122.             print day,
  123.         print ')'
  124.  
  125. def realname(host):
  126.     try:
  127.         import nis
  128.     except ImportError:
  129.         return host
  130.     try:
  131.         response = nis.match(host, 'hosts.byaddr')
  132.         words = string.split(response)
  133.         return words[1]
  134.     except nis.error:
  135.         return host
  136.  
  137. main()
  138.